package com.linkoog.devtools.code_generator.setting.configurable.ui.type;

import com.github.linkoog.model_designer.common.JCheckBoxTableCellEditor;
import com.github.linkoog.model_designer.common.JCheckBoxTableCellRenderer;

import com.linkoog.devtools.code_generator.database.domain.DbType;
import com.linkoog.devtools.code_generator.database.domain.TypeMapping;
import com.linkoog.devtools.code_generator.setting.configurable.ui.TableModelEditor;
import com.intellij.util.Function;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.table.IconTableCellRenderer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class TypeMappingTableModelEditor extends TableModelEditor<TypeMapping> {

    public TypeMappingTableModelEditor() {
        super(COLUMNS, itemEditor, "No Type mapping configured");
    }

    @Override
    public TypeMapping createElement() {
        return new TypeMapping( DbType.MYSQL, "","varchar", "String", true);
    }

    private static final ColumnInfo[] COLUMNS = {
            new ColumnInfo<TypeMapping, DbType>("数据库") {
                @Override
                public Class getColumnClass() {
                    return DbType.class;
                }

                @NotNull
                @Override
                public DbType valueOf(TypeMapping typeMapping) {
                    return typeMapping.getDbType();
                }

                @Override
                public void setValue(TypeMapping typeMapping, DbType value) {
                    typeMapping.setDbType(value);
                }

                @NotNull
                @Override
                public TableCellRenderer getRenderer(TypeMapping typeMapping) {
                    return IconTableCellRenderer.ICONABLE;
                }

                @Override
                public boolean isCellEditable(TypeMapping typeMapping) {
                    return true;
                }
            },
            new TableModelEditor.EditableColumnInfo<TypeMapping, String>("类型") {
                @NotNull
                @Override
                public String valueOf(TypeMapping typeMapping) {
                    return typeMapping.getColumnDataType();
                }

                @Override
                public void setValue(TypeMapping typeMapping, String value) {
                    typeMapping.setColumnDataType(value);
                }

                @Override
                public boolean isCellEditable(TypeMapping typeMapping) {
                    return true;
                }
            },
            new TableModelEditor.EditableColumnInfo<TypeMapping, String>("Java类型") {
                @NotNull
                @Override
                public String valueOf(TypeMapping typeMapping) {
                    return typeMapping.getJavaFieldType();
                }

                @Override
                public void setValue(TypeMapping typeMapping, String value) {
                    typeMapping.setJavaFieldType(value);
                }

                @Override
                public boolean isCellEditable(TypeMapping typeMapping) {
                    return true;
                }
            },
            new TableModelEditor.EditableColumnInfo<TypeMapping, Boolean>("是否生效") {
                @Override
                public Class getColumnClass() {
                    return Boolean.class;
                }

                @Nullable
                @Override
                public Boolean valueOf(TypeMapping typeMapping) {
                    return typeMapping.isChecked();
                }

                @Override
                public void setValue(TypeMapping typeMapping, Boolean value) {
                    typeMapping.setChecked(value);
                }

                @Override
                public boolean isCellEditable(TypeMapping typeMapping) {
                    return true;
                }

                @Override
                public @Nullable TableCellRenderer getRenderer(TypeMapping typeMapping) {
                    return new JCheckBoxTableCellRenderer();
                }

                @Override
                public @Nullable TableCellEditor getEditor(TypeMapping typeMapping) {
                    return new JCheckBoxTableCellEditor();
                }

                @Override
                public int getWidth(JTable table) {
                    return 85;
                }
            }
    };

    private static final TableModelEditor.DialogItemEditor<TypeMapping> itemEditor = new TableModelEditor.DialogItemEditor<TypeMapping>() {
        @NotNull
        @Override
        public Class<TypeMapping> getItemClass() {
            return TypeMapping.class;
        }



        @Override
        public TypeMapping clone(@NotNull TypeMapping old, boolean forInPlaceEditing) {
            return TypeMapping.copy(old);
        }

        @Override
        public void edit(@NotNull TypeMapping item, @NotNull Function<TypeMapping, TypeMapping> mutator, boolean isAdd) {
                /*BrowserSpecificSettings settings = cloneSettings(browser);
                if (settings != null && ShowSettingsUtil.getInstance().editConfigurable(browsersTable, settings.createConfigurable())) {
                    mutator.fun(browser).setSpecificSettings(settings);
                }*/
        }

        @Override
        public void applyEdited(@NotNull TypeMapping oldItem, @NotNull TypeMapping newItem) {
            //oldItem.setSpecificSettings(newItem.getSpecificSettings());
        }

        @Override
        public boolean isEditable(@NotNull TypeMapping browser) {
            //browser.getSpecificSettings() != null;
            return false;
        }

        @Override
        public boolean isRemovable(@NotNull TypeMapping item) {
            //!WebBrowserManager.getInstance().isPredefinedBrowser(item);
            return true;
        }
    };

}
